package com.ls.fw.data.bean;

import java.io.Serializable;

import com.ls.fw.data.enums.FieldType;
import com.ls.fw.data.visitor.OrderByVisitor;

/**
 * 排序规则 （默认降序）
 * @author ls
 * 2014年11月21日下午2:27:53
 */
public class Order extends Column implements Serializable, Cloneable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//排序类型
	protected OrderType type;
	
	/**
	 * 排序类型（默认降序）
	 * @author ls
	 * 2014年11月21日下午2:56:46
	 */
	public enum OrderType{
		ASC("asc"), DESC("desc");
		
		private String name;
		
		private OrderType(String name){
			this.name = name;
		}
		
		public static OrderType get(String name){
			OrderType[] opears = values();
			for (OrderType opear : opears) {
				if(opear.getName().equalsIgnoreCase(name)){
					return opear;
				}
			}
			return DESC;
		}
		public String getName() {
			return name;
		}
	}
	
	public Order(String name) {
		this(name, OrderType.DESC);
	}
	
	public Order(String name, String type) {
		this(name, OrderType.get(type));
	}
	
	public Order(String name, OrderType type) {
		super(name);
		this.type = type;
	}

	public OrderType getType() {
		return type;
	}

	public Order setType(OrderType type) {
		this.type = type;
		return this;
	}

	@Override
	public String toString() {
		return "Order [name=" + name + ", type=" + type + "]";
	}

	public void accept(OrderByVisitor visitor) {
		visitor.visit(this);
	}

	public Order setFieldType(FieldType fieldType) {
		super.setFieldType(fieldType);
		return this;
	}
}
